package com.ld.admin.controller.system;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.config.SystemStaticParams;
import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.SystemUser;
import com.ld.admin.service.user.IUserService;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestIntercept;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 用户信息
 * 
 * @ClassName SysUserController
 * @author 吕凯
 * @date 2019年01月09日 09:37:09
 *
 */
@Api(tags = "用户信息")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "user.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemSysUserController")
@RequestMapping("${shieldsb.adminPath}/system/user")
public class SysUserController extends AdminBasicController<SystemUser> {
    @Autowired
    protected IUserService userService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/user/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/system/user/sysuser_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        userService.searchUsers(request);
        return "a/system/user/sysuser_list";
    }

    @ApiOperation(value = "用户信息展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
//        SystemUser user = 
        userService.getUser(request, id);
//        userService.fetchLinks(user, SystemUser::getUserRoles, new QueryModel()); //查询关联的角色
        return "a/system/user/sysuser_show";
    }

    @ApiOperation(value = "跳转到添加/修改用户信息", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        userService.getUser(request, id);
        return "a/system/user/sysuser_update";
    }

    @ApiOperation(value = "添加/修改用户信息", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object addOrUpd(@Valid SystemUser model) {
        Result result = userService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除用户信息", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object del() {
        long id = getLong("id");
        Result result = userService.delUser(id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "冻结用户", notes = "")
    @RequestMapping(path = "/freeze", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object freeze() throws ServletException, IOException {
        long id = getLong("id");
        Result result = userService.freezeUser(id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "解除冻结用户", notes = "")
    @RequestMapping(path = "/unfreeze", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object unfreeze() throws ServletException, IOException {
        long id = getLong("id");
        Result result = userService.unfreezeUser(id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "停用用户", notes = "")
    @RequestMapping(path = "/disable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object disable() throws ServletException, IOException {
        long id = getLong("id");
        Result result = userService.disableUser(id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "启用用户", notes = "")
    @RequestMapping(path = "/enable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object enable() throws ServletException, IOException {
        long id = getLong("id");
        Result result = userService.enableUser(id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "重置用户密码", notes = "")
    @RequestMapping(value = "/pwdupdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object pwdUpdate(SystemUser user) {
        Result result = userService.resetPassword(user, getString("newPasswordConfirm"));
        Web.Request.result(request, result); // 记录日志用
        return result;
    }

    @ApiOperation(value = "跳转到设置用户角色", notes = "")
    @RequestMapping(path = "/toSetRoles", method = RequestMethod.GET)
    public String toSetRoles() {
        long id = getLong("id");
        userService.getUserRoles(request, id);
        return "a/system/user/sysuser_setroles";
    }

    @ApiOperation(value = "设置用户角色", notes = "")
    @RequestMapping(path = "/setRoles", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object setRoles(@Valid SystemUser model) {
        List<String> roleIds = getList("roleId");
        Result result = userService.saveUserRoles(model.getId(), roleIds);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "跳转到用户所拥有的权限", notes = "")
    @RequestMapping(path = "/sysuser_permission", method = RequestMethod.GET)
    public String sysuser_permission() {
        setAttributes();
        Long userId = getLong("id");
        // 超级管理员拥有所有权限
        if (userId == null || !userId.equals(SystemStaticParams.TOP_MANAGER_ID)) {
            request.setAttribute("checkPermission", true);
        } else {
            request.setAttribute("checkPermission", false);
        }
        return "a/system/user/sysuser_permission";
    }

    @ApiOperation(value = "获取用户拥有权限结果", notes = "")
    @RequestMapping(path = "/permissiondata", method = RequestMethod.GET)
    // 返回json格式
    @ResponseBody
    public Object permissiondata(String... args) throws ServletException, IOException {
        long userId = getLong("id");
        Result result = userService.getUserPermData(request, userId);
        return Web.Response.resultWithData(result);
    }
}